// pages/news/newsList/newsList.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    newsArr:[],
    pageNum:'1',//分页
    resultData:[],//数据源
    imgURL: app.globalData.nImgURL,//新闻图片总路径

  },

  //新闻点击
  newsListClick:function(e){
    var that = this;
    console.log(e.currentTarget.dataset.url);
    app.post('news/getCounts', { id: e.currentTarget.id }).then((res) => {
      console.log('success');
      console.log(res);
      wx.navigateTo({
        url: '../newsDetail/newsDetail?url=' + e.currentTarget.dataset.url,
      })
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.requestData();
  },

  //请求数据
  requestData: function () {
    var that = this;
    app.post('news/list', { pageNum: that.data.pageNum }).then((res) => {
      console.log('success');
      console.log(res);
      if (res.code == 0) {
        if (that.data.pageNum == '1'){
          that.setData({
            resultData: res.resultData,
          })
        }else{
          that.setData({
            resultData: that.data.resultData.concat(res.resultData),
          })
        }

        if (that.data.resultData.length == 0) {
          wx.showToast({
            title: '暂无数据',
            icon: 'none',
          })
        }
      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
        })
      }
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      pageNum: '1',//分页
    })
    wx.showNavigationBarLoading();
    setTimeout(function(){
      that.requestData();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    },2000);
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('加载更多');
    var that = this;
    that.data.pageNum++;
    console.log(that.data.pageNum);
    that.requestData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})